package com.student_exam_system.dao;

import com.student_exam_system.pojo.Paper;
import com.student_exam_system.pojo.Question;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

/**
 * @author fire'ge
 */
public interface PaperMapper {
    /**添加一个Paper
     * @param paper 传入paper类
     * @return int
     */
    int addPaper(Paper paper);
    /**根据id删除一个Paper
     * @param id 传入id
     * @return int
     */
    int deletePaperById(int id);

    /**更新Paper
     * @param paper 传入paper类
     * @return int
     */
    int updatePaper(Paper paper);
    /**根据id查询,返回一个Paper
     * @param id 传入id
     * @return int
     */
    Paper queryPaperById(int id);

    /**查询全部Paper,返回list集合
     * @return List<Paper>
     */
    List<Paper> queryAllPaper();


    /**通过userId查询全部Paper,返回list集合
     * @param userId 用户的id
     * @return List<Paper>
     */
    List<Paper> queryPaperByUserId(@Param("userId") int userId);


    /**
     * 获取试卷id
     * @param paperName 试卷名
     * @param paperTeacher  教师id
     * @param paperDateTime   时间
     * @return 试卷id
     */
    @Select("select max(paper.paper_id) from paper  where paper_name = #{paperName} and paper_teacher = #{paperTeacher} and datetime = #{paperDateTime}")
    int getPaperId(@Param("paperName") String paperName,@Param("paperTeacher") int paperTeacher,@Param("paperDateTime") String paperDateTime);
    /**
     * 创建试卷
     * @param paperName  试卷名
     * @param paperTeacher  创建试卷的老师
     * @param paperDateTime  创建时间
     * @return  【1：创建成功  0：创建失败】
     */
    @Insert("insert into paper values (0,#{paperTeacher},#{paperDateTime},#{paperName})")
    int newPaper(@Param("paperName") String paperName,@Param("paperTeacher") int paperTeacher,@Param("paperDateTime") String paperDateTime);

    /**
     * 查看所有的试卷【先显示试卷名字，详细的内容需要点击了名字之后进入循环出来】
     * @return paper对象
     */
    @Select("select paper_name,paper_id from exam.paper")
    @Results({
            @Result(column = "paper_id",property = "paperId"),
            @Result(column = "paper_name",property = "paperName")
    })
    List<Map<Integer,String>> selectAllPaperName();


    /**
     * 根据用户点击进去的id进行查询循环遍历出整张试卷的所有试题
     * @param paperId 试卷id
     * @return paper对象
     */
    @Select("select *from exam.paper where paper_id= #{paperId}")
    @Results(
            {
                    @Result(column = "paper_id",property = "paperId"),
                    @Result(column = "paper_name",property = "paperName"),
                    @Result(column = "paper_teacher",property = "paperTeacher"),
                    @Result(column = "datetime",property = "paperDateTime"),
                    @Result(
                            property = "questionList",
                            column = "paper_id",
                            javaType = List.class,
                            many = @Many(select = "com.student_exam_system.dao.PaperMapper.findQuestionOne")
                    )

            })
    List<Paper> selectAllPaper(int paperId);


    /**
     * 查找试卷的试题
     * @param paperId  试卷id
     * @return  paper对象
     */
    @Select("select * from exam.allPaper_Question where paperId= #{paperId}")
    @Results({
            @Result(column = "questionId",property = "questionId"),
            @Result(column = "questionContent",property = "questionContent"),
            @Result(column = "questionType",property = "questionType"),
            @Result(column = "questionScore",property = "questionScore"),
    })
    Question findQuestionOne(int paperId);



    /**
     * 自动交卷
     * @param paperId  试卷id
     * @param studentId  学生id
     * @return 【1：自动交卷成功（主要以当作考生已经考过这次考试为主，即一旦开始考试就会被视为参加考试，会自动交卷）
     *          0：自动交卷失败（当作）】
     */
    @Update("update exam.user_paper set up__sta = 1 where up_paper = #{paperId} and up_user = #{studentId} ")
    int putInPaper(@Param("paperId")int paperId,@Param("studentId")int studentId);

    /**
     * 获取开始考试时间跟考试时长
     * @param paperId 试卷id
     * @param classId  班级id
     * @return 【K,V】:【开始考试时间，考试时长（单位：时间）】
     */
    @Select("select pc.pc_time from exam.paper_class pc,exam.user_paper up " +
            "where pc.pc_paper=up.up_paper and pc.pc_class = #{classId} and pc_paper =#{paperId}")
    String getStartTime(@Param("paperId")int paperId,@Param("classId")int classId);

    /**
     * 获取该次考试时长
     * @param paperId  试卷id
     * @param classId  班级id
     * @return  考试时长
     */
    @Select("select pc.pc_examTime from exam.paper_class pc,exam.user_paper up " +
            "where pc.pc_paper=up.up_paper and pc.pc_class = #{classId} and pc_paper =#{paperId}")
    Integer getTime(@Param("paperId")int paperId,@Param("classId")int classId);

    /**
     * 获取考生此次考试的试卷的状态【1：已经提交过  0：还没提交】
     * @param studentId  学生id
     * @param paperId  试卷id
     * @return 【状态的标志性数字：1或0】
     */
    @Select("select up__sta from exam.user_paper where up_paper= #{paperId} and up_user = #{studentId}")
    int getPaperStatus(@Param("studentId")int studentId,@Param("paperId") int paperId);


}
